package com.example.node.strategy;

import com.example.ProcessFlowContext;
import com.example.domain.Product;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

@LiteflowComponent(id = "discount", name = "执行折扣策略")
@Component
@Slf4j
public class Discount extends NodeComponent {
    @Override
    public void process() throws Exception {
        ProcessFlowContext context = this.getContextBean(ProcessFlowContext.class);
        BigDecimal amount = new BigDecimal(0);
        for (Product product : context.getProductList()) {
            if ("办公用品".equals(product.getProductType())) {
                amount = amount.add(product.getPrice().multiply(new BigDecimal(0.8)));
            } else if ("家电".equals(product.getProductType())) {
                amount = amount.add(product.getPrice().multiply(new BigDecimal(0.6)));
            } else {
                amount = amount.add(product.getPrice());
            }
            amount = amount.setScale(2, RoundingMode.HALF_UP);


        }
        log.info("折扣后商品总价：{}", amount.floatValue());
        context.getStrategyMap().put("DISCOUNT", amount);
    }
}
